﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Company_OfficeAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    
        if (!IsPostBack)
        {
            this.BindDataToLanguageDropDown();
            this.SetLabelLanguage();
            this.InitData();
            this.SetValidate();
        }
    }

    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;

        lblAddress.Text = MultipleLanguage.LBL_ADDRESS[nDefaultLanguage];
        lblCompanyName.Text = MultipleLanguage.LBL_OFFICE[nDefaultLanguage];
      
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage];
        lblEmail.Text = MultipleLanguage.LBL_EMAIL[nDefaultLanguage];
        lblFax.Text = MultipleLanguage.LBL_FAX[nDefaultLanguage];
     
        lblTel.Text = MultipleLanguage.LBL_TEL[nDefaultLanguage];
        
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage];
        btnCancel.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage]; 

    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();

        // Bind Data To Language
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    private void SetValidate()
    {
        int nCurrentLanguage = Support.GetDefaultLanguage() - 1;
        rfvCompanyName.ErrorMessage = MessagesValidate.Setting_COMPANYNAME_EMPTY[nCurrentLanguage];
        rfvAddress.ErrorMessage = MessagesValidate.Setting_ADDRESS_EMPTY[nCurrentLanguage];
        revEmail.ErrorMessage = MultipleLanguage.EMAIL_WRONG[nCurrentLanguage];
    }
    private void InitData()
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);

        try
        {
            if (sQueery == Config.ACTION_EDIT)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);



                if (nItemID > 0)
                    GetData(nCurrentLang, nItemID);

                Session[Config.SESSION_LANGID] = "1";
            }
           

        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY  + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }
    }

    private void GetData(int CurrentLang, int ItemID)
    {
        // Get data to view on UI Controls

        ContactOffice objContactOffice = new ContactOffice();
        objContactOffice.LoadById(ItemID);


        ddlLanguage.SelectedValue = ConvertData.ConvertToString(CurrentLang);
        txtEmail.Text = objContactOffice.Data.Email;
    
        switch (CurrentLang)
        {
            case 1: // Vietnam
                txtAddress.Text = objContactOffice.Data.Address1 ;
                txtCompanyName.Text = objContactOffice.Data.Name1 ;
                txtFax.Text =  objContactOffice.Data.Fax1 ;
                txtTel.Text = objContactOffice.Data.Tel1;
                break;
            case 2: // English
                 txtAddress.Text = objContactOffice.Data.Address2 ;
                txtCompanyName.Text = objContactOffice.Data.Name2 ;
                txtFax.Text =  objContactOffice.Data.Fax2 ;
                txtTel.Text = objContactOffice.Data.Tel2;
                break;
            case 3: // Other Lang
                txtAddress.Text = objContactOffice.Data.Address3 ;
                txtCompanyName.Text = objContactOffice.Data.Name3 ;
                txtFax.Text =  objContactOffice.Data.Fax3 ;
                txtTel.Text = objContactOffice.Data.Tel3;
                break;
        }

      
    }

    private eContactOffice ContactOfficeEntity()
    {
        eContactOffice entityContactOffice = new eContactOffice();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            ContactOffice objContactOffice = new ContactOffice();
            objContactOffice.LoadById(nItemID);
            entityContactOffice = objContactOffice.Data;
        }
        return entityContactOffice;
    }

    private eContactOffice SetData(int CurrentLang)
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        eContactOffice eContactOfficeEntity = this.ContactOfficeEntity();
        ContactOffice objContactOffice = new ContactOffice();

        objContactOffice.Data.Email = txtEmail.Text.Trim();
     
        switch (CurrentLang)
        {
            case 1:
                objContactOffice.Data.Name1  = txtCompanyName.Text.Trim() ;
                objContactOffice.Data.Address1  = txtAddress.Text.Trim () ;
                objContactOffice.Data.Fax1  = txtFax.Text.Trim() ;
                objContactOffice.Data.Tel1 = txtTel.Text.Trim();

                objContactOffice.Data.Name2 = ConvertData.ConvertToString(eContactOfficeEntity.Name2 );
                objContactOffice.Data.Address2 = ConvertData.ConvertToString(eContactOfficeEntity.Address2 );
                objContactOffice.Data.Fax2 = ConvertData.ConvertToString(eContactOfficeEntity.Fax2 );
                objContactOffice.Data.Tel2 = ConvertData.ConvertToString(eContactOfficeEntity.Tel2 ); 

                 objContactOffice.Data.Name3 = ConvertData.ConvertToString(eContactOfficeEntity.Name3 );
                objContactOffice.Data.Address3 = ConvertData.ConvertToString(eContactOfficeEntity.Address3 );
                objContactOffice.Data.Fax3 = ConvertData.ConvertToString(eContactOfficeEntity.Fax3 );
                objContactOffice.Data.Tel3 = ConvertData.ConvertToString(eContactOfficeEntity.Tel3 ); 

                break;
            case 2:
                objContactOffice.Data.Name2  = txtCompanyName.Text.Trim() ;
                objContactOffice.Data.Address2  = txtAddress.Text.Trim () ;
                objContactOffice.Data.Fax2  = txtFax.Text.Trim() ;
                objContactOffice.Data.Tel2 = txtTel.Text.Trim();

                objContactOffice.Data.Name1 = ConvertData.ConvertToString(eContactOfficeEntity.Name1 );
                objContactOffice.Data.Address1 = ConvertData.ConvertToString(eContactOfficeEntity.Address1 );
                objContactOffice.Data.Fax1 = ConvertData.ConvertToString(eContactOfficeEntity.Fax1 );
                objContactOffice.Data.Tel1 = ConvertData.ConvertToString(eContactOfficeEntity.Tel1 ); 

                 objContactOffice.Data.Name3 = ConvertData.ConvertToString(eContactOfficeEntity.Name3 );
                objContactOffice.Data.Address3 = ConvertData.ConvertToString(eContactOfficeEntity.Address3 );
                objContactOffice.Data.Fax3 = ConvertData.ConvertToString(eContactOfficeEntity.Fax3 );
                objContactOffice.Data.Tel3 = ConvertData.ConvertToString(eContactOfficeEntity.Tel3 ); 
                break;
            case 3:
                 objContactOffice.Data.Name3  = txtCompanyName.Text.Trim() ;
                objContactOffice.Data.Address3  = txtAddress.Text.Trim () ;
                objContactOffice.Data.Fax3  = txtFax.Text.Trim() ;
                objContactOffice.Data.Tel3 = txtTel.Text.Trim();

                objContactOffice.Data.Name2 = ConvertData.ConvertToString(eContactOfficeEntity.Name2 );
                objContactOffice.Data.Address2 = ConvertData.ConvertToString(eContactOfficeEntity.Address2 );
                objContactOffice.Data.Fax2 = ConvertData.ConvertToString(eContactOfficeEntity.Fax2 );
                objContactOffice.Data.Tel2 = ConvertData.ConvertToString(eContactOfficeEntity.Tel2 ); 

                 objContactOffice.Data.Name1 = ConvertData.ConvertToString(eContactOfficeEntity.Name1 );
                objContactOffice.Data.Address1 = ConvertData.ConvertToString(eContactOfficeEntity.Address1 );
                objContactOffice.Data.Fax1 = ConvertData.ConvertToString(eContactOfficeEntity.Fax1 );
                objContactOffice.Data.Tel1 = ConvertData.ConvertToString(eContactOfficeEntity.Tel1 ); 
                break;
        }
        return objContactOffice.Data;
    }

    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
   


    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.InitData();
    }
  
    protected void lkaddnew_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY;
        Response.Redirect(sGoToPageMan);
    }
    protected void btnSave_Click1(object sender, EventArgs e)
    {
        int nActionResult = 0;
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        ContactOffice objContactOffice = new ContactOffice();
        objContactOffice.Data = SetData(nCurrentLanguage);
        string sAction = GetQuerryString(Config.ACTION);
        if (sAction == Config.ACTION_EDIT)
        {
            //Is Edit
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
            nActionResult = objContactOffice.Update(nItemID);
            if (nActionResult > 0)
            {
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage - 1]);

            }
            else
                MessageBoxss.Show(MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage - 1]);
        }
        else
        {
            // Is Insert New

            nActionResult = objContactOffice.Insert();
            if (nActionResult > 0)
            {
                string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                Response.Redirect(sURL);
            }

        }      
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY;
        Response.Redirect(sGoToPageMan);
    }
}